<template>
  <div class="my-container">
    <div v-if="user" class="header login">
      <div class="base-info">
        <div class="left">
          <van-image
            class="img"
           round
            fit="cover"
            :src="userInfo.ptoto"
          />
          <span class="user-name">{{ userInfo.name }}</span>
        </div>
        <van-button round size="mini">编辑资料</van-button>
      </div>
      <div class="data-stats">
        <div class="data-item">
          <span class="count">10</span>
          <span class="text">黑马</span>
        </div>
        <div class="data-item">
          <span class="count">10</span>
          <span class="text">关注</span>
        </div> 
        <div class="data-item">
          <span class="count">10</span>
          <span class="text">粉丝</span>
        </div> 
        <div class="data-item">
          <span class="count">10</span>
          <span class="text">获赞</span>
        </div>
      </div>
    </div>

    <div v-else class="header not-login">
      <div class="login-btn" @click="$router.push('/login')" >
        <img class="mobile-img" src="@/assets/mobile.png" alt="">
        <span class="text">登录 / 注册</span>
      </div>
    </div>

    
    <van-grid class="grid-nav" :column-num="2" clickable>
      <van-grid-item class="grid-left">
        <i slot="icon" class="iconfont icon-shoucang"></i>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="grid-right">
        <i slot="icon" class="iconfont icon-lishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>
    <van-cell-group>
    <van-cell title="消息通知" is-link />
    <van-cell title="小智同学"  is-link/>
    </van-cell-group>
    <van-cell v-if="user" @click="onLogout" title="退出登录" class="logout-cell"/>
  </div>
</template>

<script>
// 映射  直接去vuex容器里的user数据
import { mapState } from 'vuex';
import { getUserInfo } from '@/api/user'
export default {
  name: "MyIndex",
  data() {
    return {
      // 定义一个空对象接收后台获取的用户信息
      userInfo:{}
    }
  },
  computed: {
    ...mapState(['user'])
  },
  created() {
    // 如果用户登录了，则请求加载用户信息
    if (this.user) {
      this.localUserInfo()
    }
  },
  methods: {
    onLogout() {
      this.$dialog.confirm({
        title: '标题',
      })
     .then(() => {
       // on confirm
      //传值给vuex的mutation改变state
       this.$store.commit('setUser',null)
     })
     .catch(() => {
       // on cancel
     });
    },
    // 后台请求用async await
    async localUserInfo() {
      try {
        const { data } = await getUserInfo()
        console.log(data);
        this.userInfo = data.data
      } catch (err) {
        this.$toast('获取数据失败')
      }
    }
  }
}
</script>

<style scoped lang="less">
.my-container{
  .header{
    height: 200px;
    // background: url('../../assets/banner.png');
    background: url('@/assets/banner.png');
    background-size: cover;
  }
  .not-login{
    display: flex;
    justify-content: center;
    align-items: center;
    .login-btn{
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      .mobile-img{
        width:70px;
        height: 70px;
        margin-bottom: 10px;
      }
      .text{
        color: #fff;
        font-size: 14px;
      }
    }
  }
 .login{
  .base-info{
    height: 135px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 40px 17px 20px;
    // 解决盒子的内边距撑开盒子大小的问题
    box-sizing: border-box;
    
    .left{
      display: flex;
      align-items: center;
      .img{
      width: 70px;
      height: 70px;
      margin-right: 15px;
      border: 2px solid #fff;
    }
    .user-name{
      color: #fff;
    }
    }
  }
  .data-stats{
    height: 65px;
    display: flex;
    // background-color: pink;
    .data-item{
      // 可取消父盒子的高度只给子盒子设置高度，也可保留父盒子高度，子盒子高度100%
      // height: 100%;
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      color: #fff;
      .count{
        font-size: 18px;
      }
      .text{
        font-size: 12px;
      }
    }
  }
 }
 .grid-nav{
  height: 65px;
  margin-bottom: 20px;
  .iconfont{
    font-size: 23px;
  }
  .text{
    font-size: 14px;
    color: #333;
    margin-top: 7px;
  }
  .grid-left{
    color: #eb5253;
  }
  .grid-right{
    color: #ff9d1d;
  }
 }
 .logout-cell{
  text-align: center;
  color: #d86262;
  margin-top: 3px;
 }
 
}
</style>